package com.shop.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.common.core.domain.Result;
import com.shop.common.core.utils.StringUtils;
import com.shop.system.domain.User;
import com.shop.system.mapper.UserMapper;
import com.shop.system.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Log4j2
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Override
    public User findByName(String userName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,userName);
        User user = getOne(queryWrapper);
        return user;
    }

    @Override
    public List<User> show() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        List<User> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.SERIALIZABLE)
    @Override
    public Result insert(User user) {
            if (StringUtils.isNotEmpty(user.getUserName()) &&
                    baseMapper.selectCount(new LambdaQueryWrapper<User>()
                            .eq(User::getUserName, user.getUserName())) > 0){
                throw new RuntimeException("账号已经存在");
            }
            if (StringUtils.isNotEmpty(user.getPhone()) &&
                    baseMapper.selectCount(new LambdaQueryWrapper<User>()
                        .eq(User::getPhone , user.getPhone())) > 0){
                throw new RuntimeException("手机号已经存在");
            }
        int result = baseMapper.insert(user);
            if (result <= 0){
                return Result.error("添加失败");
            }
        return Result.success(result);
    }

    @Override
    public Result upd(User user) {
        if (StringUtils.isNotEmpty(user.getUserName())
                && baseMapper.selectCount(new LambdaQueryWrapper<User>()
                    .eq(User::getUserName , user.getUserName())) > 0){
            throw new RuntimeException("账号已经存在");
        }
        if (StringUtils.isNotEmpty(user.getPhone()) &&
                baseMapper.selectCount(new LambdaQueryWrapper<User>()
                        .eq(User::getPhone , user.getPhone())) > 0){
            throw new RuntimeException("手机号已经存在");
        }
        int update = baseMapper.updateById(user);
        if (update <= 0){
            return Result.error("修改失败");
        }
        return Result.success(update);
    }

    @Override
    public Result del(Integer id) {
        int byId = baseMapper.deleteById(id);
        if (byId <= 0){
            return Result.error("删除失败");
        }
        return Result.success(byId);
    }
}
